package com.xzssmk.leisure.x.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xzssmk.leisure.x.po.IssuePeriodWithBLOBs;
import com.xzssmk.leisure.x.service.IssuePeriodService;
import com.xzssmk.leisure.x.util.DataTables;

@RestController
@RequestMapping("issuePeriod")
public class IssuePeriodController {

    @Autowired
    private IssuePeriodService issuePeriodService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String add(IssuePeriodWithBLOBs issuePeriodWithBLOBs,
        @RequestParam(value = "cardPic", required = false) MultipartFile cardFacePic)
        throws IllegalStateException, IOException {

        issuePeriodWithBLOBs.setPublicState((byte)0);
        issuePeriodWithBLOBs.setCreateTime(new Date());

        String originalFilename = cardFacePic.getOriginalFilename();
        if (cardFacePic != null && originalFilename != null && originalFilename.length() > 0) {

            String pic_path = "D:\\image\\";
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            File newFile = new File(pic_path + newFileName);
            cardFacePic.transferTo(newFile);
            issuePeriodWithBLOBs.setCardFacePic(newFileName);
        }
        return JSONObject.toJSONString(issuePeriodService.add(issuePeriodWithBLOBs));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String updateRole(@ModelAttribute IssuePeriodWithBLOBs issuePeriodWithBLOBs) {
        return JSONObject.toJSONString(issuePeriodService.update(issuePeriodWithBLOBs));
    }

    @RequestMapping(value = "/getIssuePeriod", method = RequestMethod.POST,
        produces = "application/json; charset=utf-8")
    public String getById(Integer id) {
        return JSONObject.toJSONString(issuePeriodService.getById(id));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public Object delete(@RequestParam("idlist[]") List<Integer> idlist) {
        return JSONObject.toJSONString(issuePeriodService.delete(idlist));
    }

    @RequestMapping(value = "/getPageList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String getPageList(HttpServletRequest request) {
        return JSONObject.toJSONString(issuePeriodService.getPageList(DataTables.getInstance(request, null)));
    }

    @RequestMapping(value = "/getSelectList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String getSelectList() {
        return JSONObject.toJSONString(issuePeriodService.getSelectList());
    }

    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String changeStatus(@RequestParam(value = "id", required = true) Integer issuePeriodId,
        @RequestParam(value = "status", required = true) Integer publicState) {
        return JSONObject.toJSONString(issuePeriodService.updatePublicState(issuePeriodId, publicState));
    }

    @RequestMapping(value = "/issuePeriod-list.html")
    public ModelAndView issuePeriod() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("issuePeriod-list");
        return modelAndView;
    }

    @RequestMapping(value = "/issuePeriod-add.html")
    public ModelAndView addIssuePeriod() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("issuePeriod-add");
        return modelAndView;
    }
}
